$(function() {
	var baseUrl = $("#project_base_url").attr("content");
	$("#form").validate({
		rules : {
			"captcha" : {
				required : true,
				cqrcb : true
			},
			"password" : {
				required : true,
				password : true
			}
		},
		messages : {
			"captcha" : {
				required : "请输入验证码"
			},
			"password" : {
				required : "请输入支付密码"
			}
		}
	});
	$("#submit").click(function() {
		if (!isRun) {
			isRun = true;
 			if ($("#form").valid()) {
				console.log($("#form").serializeJson());
				showLoading("请稍候，正在支付…")
				var jsonParams = $("#form").serializeJson();
				jsonParams.password =$.md5(jsonParams.password);
				$.post(baseUrl + "/subscribe/pay",jsonParams,function(data) {
					console.log(data);
					isRun = false;
					hideLoading();
					if (data.state) {
						successTimeUrl("支付成功", 5, data.callBack);
					} else {
						errorTime(data.info, 5);
					}
				}).error(function() {
					isRun = false;
					hideLoading();
					errorTime("连接服务器失败，请稍候重试", 5);
				});
			} else {
				isRun = false;
			}
		}
	});
	
	$("#sendRegisterCode").click(function() {
		if (!isRunSendSms) {
			isRunSendSms = true;
			$("#sendRegisterCode").html("正在发送...");
			$.post(baseUrl + "/subscribe/sendSms", {
				"dealno" : $("#dealno").val()
			}, function(data) {
				console.log(data);
				if (data.state) {
					remainTime();
					$("#captcha_msg").show();
					//$("#sendRegisterCode").addClass("obtained");
					$("#seqCode").val(data.serialNo);
				} else {
					isRunSendSms = false;
					$("#seqCode").val("");
					$("#sendRegisterCode").html("获取验证码");
					errorTime(data.info, 3);
				}
			}).error(function() {
				isRunSendSms = false;
				$("#sendRegisterCode").html("获取验证码");
				errorTime("获取验证码失败,请稍后重试", 3);
			});
		} else {
			$("#form").valid();
		}
	});
});
var isRun = false;
var isRunSendSms = false;
var i = 180;
function remainTime() {
	if (i == 0) {
		i = 180;
		$("#sendRegisterCode").html("获取验证码");
		$("#sendRegisterCode").removeClass("obtained");
		isRunSendSms=false;
	} else {
		$("#sendRegisterCode").html((i--) + "秒后获取");
		setTimeout("remainTime()", 1000);
	}
}